import api from '../../../http/api';
import CustomNavBar from '@/components/CustomNavBar/CustomNavBar.vue';
	// import uniIcons from '@/components/uni-icons/uni-icons.vue';
	export default {
		components: { CustomNavBar },
		data() {
			return {
				// 我的地址 (固定值)
				myAddress: '',
				
				formData: {
					toAddress: '',
					amount: '',
					password: ''
				},
				
				errors: {
					toAddress: '',
					amount: '',
					password: ''
				},
				userAllInfo:{}
			}
		},
		computed: {
			isFormValid() {
				return Object.values(this.errors).every(error => !error) &&
					Object.values(this.formData).every(val => val !== '');
			},
			totalNavHeight() {
			  const app = getApp();
			  // 确保 globalData 存在，提供默认值
			  return (app.globalData?.statusBarHeight || 0) + (app.globalData?.navBarHeight || 44);
			}
		},
		onLoad() {
			  this.getUerInfo();
		},
		methods: {
			//获取用户数据
			getUerInfo(){
				api.getUerInfo().then(res=>{
					if(res.code==1){
						this.userAllInfo = res.data;
					}
				})
			},
			// 复制地址功能
			copyAddress() {
				uni.setClipboardData({
					data: this.myAddress,
					success: () => {
						uni.showToast({
							title: '地址已复制',
							icon: 'success'
						});
					}
				});
			},
			
			// 验证转账地址
			validateAddress() {
				const address = this.formData.toAddress;
				if (!address) {
					this.errors.toAddress = '地址不能为空';
				} else {
					this.errors.toAddress = '';
				}
			},
			
			// 验证金额
			validateAmount() {
				const amount = this.formData.amount;
				if (!amount) {
					this.errors.amount = '金额不能为空';
				} else if (isNaN(amount) || parseFloat(amount) <= 0) {
					this.errors.amount = '请输入有效金额';
				} else if (parseFloat(amount) > 100000) {
					this.errors.amount = '单笔转账限额100,000';
				} else if (!/^\d+(\.\d{1,8})?$/.test(amount)) {
					this.errors.amount = '最多保留8位小数';
				} else {
					this.errors.amount = '';
				}
			},
			
			// 验证交易密码
			validatePassword() {
				const password = this.formData.password;
				if (!password) {
					this.errors.password = '密码不能为空';
				} else {
					this.errors.password = '';
				}
			},
			
			submitForm() {
				// 提交前的最终验证
				this.validateAddress();
				this.validateAmount();
				this.validatePassword();
				
				if (this.isFormValid) {
					api.transferAccounts({
						money:this.formData.amount,
						wallet_address:this.formData.toAddress,
						pay_password:this.formData.password
					}).then(res=>{
						if(res.code == 1){
							uni.showToast({
								title: '转账申请提交成功',
								icon: 'success'
							});
							this.formData = {
								toAddress: '',
								amount: '',
								password: ''
							}
						}else{
							uni.showToast({
								title: res.msg,
								icon: 'none'
							});
						}
					}).catch(err=>{
						console.log(err)
					})
					
					
					
				} else {
					uni.showToast({
						title: '请检查表单',
						icon: 'none'
					});
				}
			}
		}
	}